/**
 * NextVisual
 */
/**
 * NextVisual
 */
package net.nextvisual.report.start;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import net.abbp.swing.util.GuiUtils;
import net.nextvisual.swing.comp.CheckBoxList;
import net.nextvisual.swing.comp.CheckBoxListEntry;

/**
 * 选择列面板
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since 销售宝 2.0
 * 
 *        <pre>
 * 历史：
 *      建立: 2013-8-29 lexloo
 * </pre>
 */
public class SelectColumnPane extends JPanel {
    /**
     * UID
     */
    private static final long serialVersionUID = -6697856696300499706L;
    public static final int OK = 1;
    public static final int CANCEL = 0;

    private int result = CANCEL;
    private CheckBoxList list = new CheckBoxList();

    public SelectColumnPane() {
        this.setLayout(new BorderLayout());
        this.add(new JLabel("选择列"), BorderLayout.NORTH);
        this.add(new JScrollPane(list));

        JPanel pnlBtns = new JPanel();
        JButton btnOK = new JButton("确定");
        btnOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setResult(OK);

                GuiUtils.getParentWindow(SelectColumnPane.this).dispose();
            }
        });
        pnlBtns.add(btnOK);

        JButton btnCancel = new JButton("取消");
        btnCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setResult(CANCEL);

                GuiUtils.getParentWindow(SelectColumnPane.this).dispose();
            }
        });
        pnlBtns.add(btnCancel);
        this.add(pnlBtns, BorderLayout.SOUTH);
    }

    /**
     * 设置列
     * 
     * @param items 列元素
     * @param status 状态
     */
    public void setColumns(List<String> items, List<Boolean> status) {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0, size = items.size(); i < size; i++) {
            model.addElement(new CheckBoxListEntry(items.get(i), status.get(i)));
        }
        list.setModel(model);
    }

    /**
     * 获取选择状态
     * 
     * @return 选择状态
     */
    public List<Boolean> getSelectStatus() {
        return list.getCheckedStatus();
    }

    /**
     * 在Dialog中显示
     * 
     * @param parent 父窗体
     * @param title 标题
     */
    public void showInDialog(Window parent, String title) {
        JDialog dlg = new JDialog(parent);
        dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dlg.setModal(true);
        dlg.setSize(new Dimension(240, 360));
        dlg.setResizable(false);
        dlg.setTitle(title);
        dlg.add(this);
        GuiUtils.centerWindow(dlg);

        dlg.setVisible(true);
    }

    /**
     * @return the result
     */
    public int getResult() {
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(int result) {
        this.result = result;
    }
}
